package functions.ems_base_data;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.LoggerFactory;
import com.beans.AccountBean;
import com.crc.mam.BaseCase;
import coms.Constants;
import coms.JSONToBean;
import frame.HttpHelper;

public class Account extends BaseCase{
  private org.slf4j.Logger logger = LoggerFactory.getLogger(Account.class);
  
  public List<AccountBean> findPage(String buCode){
    logger.info("findPage   --- start -----");
    List<AccountBean> allAccount  = null;
    String url = Constants.ACCOUNT_FIND_PAGE_URL;
    String params = "{\"page\":{\"currentPage\":1,\"pageSize\":10,\"recordCount\":0},"
        + "\"params\":{\"buCode\":\""+buCode+"\",\"accountItemCode\":\"\",\"accountItemName\":\"\","
            + "\"childAccountItemCode\":\"\",\"childAccountItemName\":\"\",\"securityEnabled\":\"0\","
            + "\"selectType\":\"0\",\"accountLevel\":\"\",\"effectiveDateEnd\":\"\",\"effectiveDateStart\":\"\","
            + "\"emsIsValidity\":\"\",\"isParentValue\":\"\",\"itemProperty\":\"\",\"parentCode\":\"\",\"rate\":0,"
            + "\"sbuCode\":\"\",\"sbuName\":\"\",\"sts\":\"\"},"
            + "\"sortDTO\":{\"fieldName\":\"\",\"orderBy\":\"\"}}";
    try {
      result = HttpHelper.post(params, url);
      status = HttpHelper.isOk;
      logger.info("findPage --- " + result);
      if(status) {
        try {
          JSONObject object = new JSONObject(result);
          int total = object.getInt("total");
          if(total >0) {
            allAccount = new ArrayList<AccountBean>();
            JSONArray array = object.getJSONArray("list");
            for(int i=0;i<array.length();i++) {
              JSONObject datas = array.getJSONObject(i);
              String jsonDatas = datas.toString();
              AccountBean accountBean = JSONToBean.parseJson2Object(AccountBean.class, jsonDatas);
              allAccount.add(accountBean);
            }
          }else {
            remark ="返回结果为空";
          }
        }catch(JSONException e) {
          e.printStackTrace();
          remark = "JSON解析异常";
        }
        
      }else {
        try {
          JSONObject object = new JSONObject(result);
          remark = object.getString("message");
        }catch(JSONException e) {
          e.printStackTrace();
          remark = "JSON解析错误";
        }
      }
    }catch(Exception e) {
      e.printStackTrace();
      status = false;
      remark = "系统异常";
    }
    logger.info("findPage   --- end -----");
    return allAccount;
  }
  
  public AccountBean getRandomAccountBean(String buCode) {
    AccountBean accountBean = null;
    List<AccountBean> accountList = findPage(buCode);
    if(accountList != null && accountList.size()>0) {
     int size = accountList.size();
     if(size == 1) {
       accountBean = accountList.get(0);
     }else {
       Random dom = new Random(size);
       int nextInt = dom.nextInt(size -1);
       accountBean = accountList.get(nextInt);
     }    
    }
    return accountBean;
  }
  
  public AccountBean findOneLenderAccount(String buCode,String categoryCode ,String businessLineCode,String companyCode) {
    logger.info("findOneLenderAccount   --- start -----");
    AccountBean accountBean = null;
    String params = "{\"buCode\":\""+buCode+"\",\"categoryCode\":\""+categoryCode+"\",\"businessLineCode\":\""+businessLineCode+"\",\"companyCode\":\""+companyCode+"\"}";
    String url = Constants.FIND_ONE_LENDER_ACCOUNT_URL;
    try {
      result = HttpHelper.post(params, url);
      status = HttpHelper.isOk;
      logger.info("findOneLenderAccount  --- "  + result);
      if(status) {
        try {
          result = result.replace("childrenAccountItemCode", "childAccountItemCode");
          result = result.replace("childrenAccountItemName", "childAccountItemName");
          accountBean = JSONToBean.parseJson2Object(AccountBean.class, result);
          remark = accountBean.toString();
        }catch(JSONException e) {
          e.printStackTrace();
          remark = "JSON解析异常";
        }
      }else {
        try {
          JSONObject object = new JSONObject(result);
          remark = object.getString("message");
        }catch(JSONException e) {
          e.printStackTrace();
          status = false;
          remark = "JSON解析错误";
        }
      }
    }catch(Exception e) {
      e.printStackTrace();
      remark = "系统异常";
      status = false;
    }
    logger.info("findOneLenderAccount   --- end -----");
    return accountBean;
  }
}
